﻿using System;
using System.Web;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Text;
using KCL.Code.Data;
using KCL.Code.Entities;

namespace KCL.Organization
{
	public class CustomerHandler : IHttpHandler
	{
		#region IHttpHandler Members
		public void ProcessRequest(HttpContext context)
		{
			// get parameters from query string
			var name = context.Request.Form["name"] != null ? context.Request.Form["name"].Trim().Replace("'", "''") : "";
			var address = context.Request.Form["address"] != null ? context.Request.Form["address"].Trim().Replace("'", "''") : "";
			var contactAddress = context.Request.Form["contactAddress"] != null ? context.Request.Form["contactAddress"].Trim().Replace("'", "''") : "";
			var contactPerson = context.Request.Form["contactPerson"] != null ? context.Request.Form["contactPerson"].Trim().Replace("'", "''") : "";
			var phone = context.Request.Form["phone"] != null ? context.Request.Form["phone"].Trim().Replace("'", "''") : "";
			var mobile = context.Request.Form["mobile"] != null ? context.Request.Form["mobile"].Trim().Replace("'", "''") : "";
			var fax = context.Request.Form["fax"] != null ? context.Request.Form["fax"].Trim().Replace("'", "''") : "";
			var accountNumber = context.Request.Form["accountNumber"] != null ? context.Request.Form["accountNumber"].Trim().Replace("'", "''") : "";
			var bank = context.Request.Form["bank"] != null ? context.Request.Form["bank"].Trim().Replace("'", "''") : "";
			var taxNumber = context.Request.Form["taxNumber"] != null ? context.Request.Form["taxNumber"].Trim().Replace("'", "''") : "";
			var director = context.Request.Form["director"] != null ? context.Request.Form["director"].Trim().Replace("'", "''") : "";
			var id = context.Request.Form["id"] != null ? context.Request.Form["id"] : "";
			var action = context.Request.Form["action"] != null ? context.Request.Form["action"] : "";
			var term = context.Request.Params["term"] != null ? context.Request.Params["term"].Trim().Replace("'", "''") : "";
			string customerCode = "";
			context.Response.ContentType = "application/html";

			List<Customer> foundCustomers = new List<Customer>();
			CustomerDAO customerDao = new CustomerDAO();
			if (!string.IsNullOrEmpty(term))
			{
				foundCustomers = customerDao.SearchCustomer(term);
				StringBuilder strBuilder = new StringBuilder("[");
				foreach (Customer customer in foundCustomers)
				{
					strBuilder.AppendFormat("{{\"id\":\"{0}\",\"name\":\"{1}\",\"address\":\"{2}\",\"contactAddress\":\"{3}\",\"contactPerson\":\"{4}\",\"phone\":\"{5}\",\"mobile\":\"{6}\",\"fax\":\"{7}\",\"accountNumber\":\"{8}\",\"bank\":\"{9}\",\"taxNumber\":\"{10}\",\"director\":\"{11}\"}},",
						customer.id, customer.name, customer.address, customer.contactAddress, customer.contactPerson, customer.phone, customer.mobile, customer.fax, customer.accountNumber, customer.bank, customer.taxNumber, customer.director);
				}
				strBuilder = strBuilder.Remove(strBuilder.Length - 1, 1);
				strBuilder.Append("]");
				context.Response.ContentType = "application/json";
				context.Response.Write(strBuilder.ToString());
				return;
			}
			if (action != "delete" && (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(address)))
			{
				context.Response.Write("alert(\"Tên khách hàng & địa chỉ phải khác rỗng!\");");
				return;
			}
			try
			{
				if (action == "create")
					customerCode = customerDao.CreateCustomer(name, address, contactAddress, contactPerson, phone, mobile, fax, accountNumber, bank, taxNumber, director);
				else if (action == "edit")
					customerDao.UpdateCustomer(id, name, address, contactAddress, contactPerson, phone, mobile, fax, accountNumber, bank, taxNumber, director);
				else if (action == "delete")
					customerDao.DeleteCustomer(id);
			}
			catch (Exception ex)
			{
				context.Response.Write(string.Format("alert(\"Có lỗi: {0}\");", ex.Message.Replace("\r\n", "\\n")));
				return;
			}

			if (action == "create")
				context.Response.Write(string.Format("$('#divCreateCustomer').dialog('close'); alert('Thêm khách hàng mới thành công.\\nMã khách hàng là {0}'); location.reload(true);", customerCode));
			else if (action == "edit")
				context.Response.Write(string.Format("$('#divCreateCustomer').dialog('close'); alert('Chỉnh sửa khách hàng thành công.'); RefreshCustomer({0});", id));
			else if (action == "delete")
				context.Response.Write("alert('Xóa khách hàng thành công.'); location.reload(true);");
		}

		public bool IsReusable
		{
			get
			{
				return false;
			}
		}

		#endregion
	}
}